package com.maybach.core.flow.handler;

import com.maybach.common.enums.PartnerStatusEnum;
import com.maybach.common.model.PartnerDO;
import com.maybach.common.model.UserDO;
import com.maybach.common.po.EnterpriseCertificatePO;
import com.maybach.common.query.PartnerQuery;
import com.maybach.core.manager.PartnerManager;
import com.maybach.core.manager.UserManager;
import com.maybach.dubbo.api.enums.AuditStatusEnum;
import com.maybach.util.AssertUtil;
import com.maybach.util.ImgUrlUtil;
import com.maybach.workflow.common.api.IFlowHandler;
import com.maybach.workflow.common.api.WorkFlowContext;
import com.maybach.workflow.common.api.enums.WFContextKeyEnum;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by houenxun on 16/1/28.
 * <p/>
 * 完成企业信息认证进行后续操作，将身份证等个人信息拷贝的个人表中
 */
@Component
public class FinishEnterpriseCertificate extends FinishSimpleFlow implements IFlowHandler {
    @Autowired
    protected PartnerManager partnerManager;
    @Autowired
    protected UserManager userManager;

    @Override
    public void handle(WorkFlowContext context) {
        super.handle(context);
        EnterpriseCertificatePO certificatePO = (EnterpriseCertificatePO) context.getBizData().get(WFContextKeyEnum.flowApplyContent.name());
        AssertUtil.notNull(certificatePO, "PersonCertificatePO by flowId=" + context.getThisFlow().getId());
        UserDO userDO = userManager.selectByPrimaryKey(certificatePO.getUserId());

        PartnerQuery query = new PartnerQuery();
        PartnerQuery.Criteria criteria = query.or();
        criteria.andCertificateCodeEqualTo(certificatePO.getCertificateCode());
        criteria.andStatusNotEqualTo(PartnerStatusEnum.logical_delete.getCode()); // 排除掉逻辑删除的

        PartnerDO partnerDO = null;

        List<PartnerDO> list = partnerManager.selectByQuery(query);
        if (CollectionUtils.isEmpty(list)) { // 新增partner
            partnerDO = this.assemblyPartner(certificatePO, userDO);
            partnerManager.insertSelective(partnerDO);
        } else if (list.size() > 1) { // 数据重复
            throw new RuntimeException("数据重复");
        } else {
            partnerDO = list.get(0);
        }

        userDO.setPartnerId(partnerDO.getId());
        userManager.updateByPrimaryKeySelective(userDO);
    }

    protected PartnerDO assemblyPartner(EnterpriseCertificatePO certificatePO, UserDO userDO) {
        PartnerDO partnerDO = new PartnerDO();
        partnerDO.setCertificateCode(certificatePO.getCertificateCode());
        partnerDO.setStatus(PartnerStatusEnum.normal.getCode());
        partnerDO.setCorpName(certificatePO.getCorpName());
        partnerDO.setCityName(certificatePO.getCityName());
        partnerDO.setContactsName(certificatePO.getContactsName());
        partnerDO.setContactsPhone(certificatePO.getContactsPhone());
        partnerDO.setCorpLogoImgs(ImgUrlUtil.mergeList(certificatePO.getCorpLogoImgs()));
        partnerDO.setCityId(certificatePO.getCity());
        partnerDO.setProviceId(certificatePO.getProvince());
        partnerDO.setCityName(certificatePO.getCityName());
        partnerDO.setDetailAddress(certificatePO.getDetailAddress());
        partnerDO.setLegalCertImgs(ImgUrlUtil.mergeList(certificatePO.getLegalImgs()));
        partnerDO.setAccountBalance(0L);
        partnerDO.setIsVerify(AuditStatusEnum.end.getStatus());
        //partnerDO.setContractImgs(ImgUrlUtil.mergeList(certificatePO.getCon)certificatePO.getCont);

        partnerDO.setMemo(certificatePO.getMemo());
        partnerDO.setStoreImgs(ImgUrlUtil.mergeList(certificatePO.getStoreImgs()));
        partnerDO.setCreatorUser(userDO.getLoginName());
        partnerDO.setCredentialsImgs(ImgUrlUtil.mergeList(certificatePO.getCredentialsImgs()));
        partnerDO.setCorpDescription(certificatePO.getCorpDescription());
        partnerDO.setMemo(certificatePO.getPartnerMemo());
        partnerDO.setManager(certificatePO.getManager());
        return partnerDO;
    }

    @Override
    protected String getHandlerKey() {
        return "enterprise";
    }
}
